package com.ruoyi.web.qualityController;


import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.quality.QualityReceipts;
import com.ruoyi.system.domain.quality.QualityReceiptsDetail;
import com.ruoyi.system.mapper.QualityReceiptsDetailMapper;
import com.ruoyi.system.service.IQualityReceiptsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 质检单Controller
 *
 * @author ruoyi
 * @date 2023-03-09
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Api(tags ="质检单")
@RestController
@RequestMapping("/quality/receipts" )
public class QualityReceiptsController extends BaseController {

    @Autowired
    private final IQualityReceiptsService iQualityReceiptsService;


    @Autowired
    private TokenService tokenService;

    @Resource
    private QualityReceiptsDetailMapper qualityReceiptsDetailMapper;

    /**
     * 查询质检单列表
     */
    //@PreAuthorize("@ss.hasPermi('quality:receipts:list')")
    @GetMapping("/list")
        @ApiOperation(value = "查询质检单列表")
        public TableDataInfo list(QualityReceipts qualityReceipts)
    {
        startPage();
        LambdaQueryWrapper<QualityReceipts> lqw = new LambdaQueryWrapper<QualityReceipts>();
        if (StringUtils.isNotBlank(qualityReceipts.getName())){
            lqw.like(QualityReceipts::getName ,qualityReceipts.getName());
        }
        if (StringUtils.isNotBlank(qualityReceipts.getCode())){
            lqw.like(QualityReceipts::getCode ,qualityReceipts.getCode());
        }
        if (qualityReceipts.getStatus() != null){
            lqw.eq(QualityReceipts::getStatus ,qualityReceipts.getStatus());
        }
        if (StringUtils.isNotBlank(qualityReceipts.getResult())){
            lqw.like(QualityReceipts::getResult ,qualityReceipts.getResult());
        }
        if (qualityReceipts.getStation() != null){
            lqw.eq(QualityReceipts::getStation ,qualityReceipts.getStation());
        }
        if (StrUtil.isNotEmpty(qualityReceipts.getStationName())){
            lqw.apply("b.shop_name like '%"+qualityReceipts.getStationName()+"%'");
        }
        if (StringUtils.isNotBlank(qualityReceipts.getWorkOrderNumber())){
            lqw.like(QualityReceipts::getWorkOrderNumber ,qualityReceipts.getWorkOrderNumber());
        }
        if (qualityReceipts.getCreateUser() != null){
            lqw.eq(QualityReceipts::getCreateUser ,qualityReceipts.getCreateUser());
        }
        if (StrUtil.isNotEmpty(qualityReceipts.getCreateUserName())){
            lqw.apply("c.user_name like '%"+qualityReceipts.getCreateUserName()+"%'");
        }
        if (qualityReceipts.getMaterialId() != null){
            lqw.eq(QualityReceipts::getMaterialId ,qualityReceipts.getMaterialId());
        }
        if (StrUtil.isNotEmpty(qualityReceipts.getMaterialName())){
            lqw.apply("d.material_name like '%"+qualityReceipts.getMaterialName()+"%'");
        }
        if (qualityReceipts.getCriterion() != null){
            lqw.eq(QualityReceipts::getCriterion ,qualityReceipts.getCriterion());
        }
        if (StrUtil.isNotEmpty(qualityReceipts.getCriterionName())){
            lqw.apply("e.dict_value like '%"+qualityReceipts.getCriterionName()+"%'");
        }
        if (qualityReceipts.getType() != null){
            lqw.eq(QualityReceipts::getType ,qualityReceipts.getType());
        }
        if (StrUtil.isNotEmpty(qualityReceipts.getTypeName())){
            lqw.apply("f.dict_value like '%"+qualityReceipts.getTypeName()+"%'");
        }
        if (StringUtils.isNotBlank(qualityReceipts.getRemark())){
            lqw.like(QualityReceipts::getRemark ,qualityReceipts.getRemark());
        }

        List<QualityReceipts> list = iQualityReceiptsService.listParam(lqw);
        return getDataTable(list);
    }

    /**
     * 导出质检单列表
     */
    //@PreAuthorize("@ss.hasPermi('quality:receipts:export')" )
    @Log(title = "质检单" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
                @ApiOperation(value = "导出质检单列表")
            public AjaxResult export(QualityReceipts qualityReceipts) {
        LambdaQueryWrapper<QualityReceipts> lqw = new LambdaQueryWrapper<QualityReceipts>(qualityReceipts);
        List<QualityReceipts> list = iQualityReceiptsService.list(lqw);
        ExcelUtil<QualityReceipts> util = new ExcelUtil<QualityReceipts>(QualityReceipts. class);
        return util.exportExcel(list, "receipts" );
    }

    /**
     * 获取质检单详细信息
     */
    //@PreAuthorize("@ss.hasPermi('quality:receipts:query')" )
    @GetMapping(value = "/{id}" )
                @ApiOperation(value = "获取质检单详细信息")
            public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iQualityReceiptsService.getById(id));
    }

    /**
     * 新增质检单
     */
    //@PreAuthorize("@ss.hasPermi('quality:receipts:add')" )
    @Log(title = "质检单" , businessType = BusinessType.INSERT)
    @PostMapping
                @ApiOperation(value = "新增质检单")
            public AjaxResult add(@RequestBody QualityReceipts qualityReceipts) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return toAjax(iQualityReceiptsService.saveAndCheck(qualityReceipts,loginUser) ? 1 : 0);
    }

//    /**
//     * 修改质检单
//     */
//    //@PreAuthorize("@ss.hasPermi('quality:receipts:edit')" )
//    @Log(title = "质检单" , businessType = BusinessType.UPDATE)
//    @PutMapping
//                @ApiOperation(value = "修改质检单")
//            public AjaxResult edit(@RequestBody QualityReceipts qualityReceipts) {
//        return toAjax(iQualityReceiptsService.updateById(qualityReceipts) ? 1 : 0);
//    }

    /**
     * 删除质检单
     */
    //@PreAuthorize("@ss.hasPermi('quality:receipts:remove')" )
    @Log(title = "质检单" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
                @ApiOperation(value = "删除质检单")
            public AjaxResult remove(@PathVariable Long[] ids) {
        qualityReceiptsDetailMapper.delete(new QueryWrapper<QualityReceiptsDetail>().lambda().in(QualityReceiptsDetail::getReceiptsId,ids));
        return toAjax( iQualityReceiptsService.removeByIds(Arrays.asList(ids))? 1 : 0);
    }
}
